package com.corx.network.mysqlstorage.repository;

import com.corx.network.mysqlstorage.common.JdbcPagingSupport;
import com.corx.network.mysqlstorage.common.PageQuery;
import com.corx.network.mysqlstorage.common.PageResult;
import com.corx.network.mysqlstorage.common.SqlQueryBuilder;
import com.corx.network.mysqlstorage.model.DeviceModelField;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.Optional;

/**
 * @author gx
 * @version 1.0.0
 */
public class JdbcDeviceModelFieldRepository extends JdbcPagingSupport<DeviceModelField> implements DeviceModelFieldRepository {

    public JdbcDeviceModelFieldRepository(JdbcTemplate jdbcTemplate) {
        super(jdbcTemplate);
    }

    @Override
    protected RowMapper<DeviceModelField> getRowMapper() {
        return (rs, rowNum) -> {
            DeviceModelField data = new DeviceModelField();
            data.setId(rs.getLong("id"));
            data.setModelId(rs.getLong("model_id"));
            data.setFieldName(rs.getString("field_name"));
            data.setFieldType(rs.getString("field_type"));
            data.setDisplayName(rs.getString("display_name"));
            data.setJsonPath(rs.getString("json_path"));
            data.setDescription(rs.getString("description"));
            data.setSort(rs.getInt("sort"));
            data.setCreateTime(rs.getTimestamp("create_time"));
            data.setUpdateTime(rs.getTimestamp("update_time"));
            return data;
        };
    }

    @Override
    public void save(@NonNull DeviceModelField model) {
        String sql = "INSERT INTO corx_device_model_field (model_id, field_name, field_type, display_name, json_path, description, sort, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW())";
        jdbcTemplate.update(sql, model.getModelId(), model.getFieldName(), model.getFieldType(), model.getDisplayName(), model.getJsonPath(), model.getDescription(), model.getSort());
    }

    @Override
    public Optional<DeviceModelField> findById(@NonNull Long id) {
        List<DeviceModelField> list = jdbcTemplate.query("SELECT * FROM corx_device_model_field WHERE id = ?", getRowMapper(), id);
        return Optional.ofNullable(DataAccessUtils.singleResult(list));
    }

    @Override
    public List<DeviceModelField> findByModelId(@NonNull Long modelId) {
        List<DeviceModelField> list = jdbcTemplate.query("SELECT * FROM corx_device_model_field WHERE model_id = ?", getRowMapper(), modelId);
        return list;
    }

    @Override
    public void update(@NonNull DeviceModelField model) {
        String sql = "UPDATE corx_device_model_field SET model_id = ?, field_name = ?, field_type = ?, display_name = ?, json_path = ?, description = ?, sort=?, update_time = NOW() WHERE id = ?";
        jdbcTemplate.update(sql, model.getModelId(), model.getFieldName(), model.getFieldType(), model.getDisplayName(), model.getJsonPath(), model.getDescription(), model.getSort(), model.getId());
    }

    @Override
    public void delete(@NonNull Long id) {
        jdbcTemplate.update("DELETE FROM corx_device_model_field WHERE id = ?", id);
    }

    @Override
    public PageResult<DeviceModelField> findByPage(DeviceModelField model, PageQuery pageQuery) {
        SqlQueryBuilder queryBuilder = new SqlQueryBuilder("SELECT * FROM corx_device_model_field");

        if (model.getModelId() != null) {
            queryBuilder.whereEquals("model_id", model.getModelId());
        }

        if (model.getFieldName() != null && !model.getFieldName().isEmpty()) {
            queryBuilder.whereLike("field_name", model.getFieldName());
        }

        if (model.getFieldType() != null && !model.getFieldType().isEmpty()) {
            queryBuilder.whereEquals("field_type", model.getFieldType());
        }

        if (model.getDisplayName() != null && !model.getDisplayName().isEmpty()) {
            queryBuilder.whereLike("display_name", model.getDisplayName());
        }

        if (pageQuery.hasSort()) {
            queryBuilder.orderBy(pageQuery.orderBy());
        }

        // 构建 SQL 和参数
        String baseSql = queryBuilder.getSql();
        Object[] params = queryBuilder.getParams();


        return findByPage(baseSql, params, pageQuery);
    }
}

